/*
 *@description:用户管理
 *@Author: 修改自角色管理代码
 *@Date: 2025-04-02
 *@Update: 2025-04-02
 *@Version: V1.0.0
*/
<template>
  <Card>
    <div class="user_box">
      <div class="header">
        <div class="btn">
          <el-button size="mini" type="primary" @click="showAdd">添加</el-button>
        </div>
      </div>
      <div class="content">
        <el-table
          :data="tableData"
          size="small"
          row-key="id"
          style="width: 100%">
          <el-table-column
            label="用户名"
            prop="username">
          </el-table-column>
          <el-table-column
            label="邮箱"
            prop="email">
          </el-table-column>
          <el-table-column
            label="角色"
            prop="role">
          </el-table-column>
          <el-table-column
            label="状态">
            <template slot-scope="scope">
              <el-tag type="success" size="mini" effect="dark" v-if="scope.row.status == '启用'">{{ scope.row.status }}</el-tag>
              <el-tag type="warning" size="mini" effect="dark" v-else>{{ scope.row.status }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="100">
            <template slot-scope="scope">
              <el-button class="columnBtn1" type="text" size="small" @click="del(scope.row)">删除</el-button>
              <el-button class="columnBtn2" type="text" size="small" @click="edit(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- dialog -->
      <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="用户名">
            <el-input v-model="form.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色">
            <el-input v-model="form.role" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="用户状态">
            <el-select v-model="form.status" placeholder="请选择状态">
              <el-option label="启用" value="启用"></el-option>
              <el-option label="禁用" value="禁用"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUser">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </Card>
</template>

<script>
import Card from '@/components/Card/index.vue'
import instance from '@/utils/instance';
export default {
  data() {
    return {
      tableData: [
        { id: 1, username: 'user1', email: 'user1@example.com', role: '普通用户', status: '启用' },
        { id: 2, username: 'user2', email: 'user2@example.com', role: '普通用户', status: '启用' },
        { id: 3, username: 'user3', email: 'user3@example.com', role: '访客', status: '禁用' },
        { id: 4, username: 'user4', email: 'user4@example.com', role: '访客', status: '启用' },
        { id: 5, username: 'user5', email: 'user5@example.com', role: '超级用户', status: '启用' },
        { id: 6, username: 'user6', email: 'user6@example.com', role: '普通用户', status: '启用' },
        { id: 7, username: 'user7', email: 'user7@example.com', role: '测试员', status: '启用' },
        { id: 8, username: 'user8', email: 'user8@example.com', role: '普通用户', status: '启用' }
      ],
      title: '',
      form: {},
      dialogFormVisible: false
    }
  },
  methods: {
    getlist() {
      instance.get('/users/list').then(res => {
        if (res.data.data && res.data.data.length > 0) {
          this.tableData = res.data.data;
        }
      }).catch(error => {
        console.error('获取数据失败:', error);
      });
    },
    // 删除
    del(scope) {
      if (confirm('确定要删除这个用户吗？')) {
        instance.delete(`/users/delete/${scope.id}`).then(res => {
          console.log(res);
          this.getlist();
        }).catch(error => {
          console.error('删除失败:', error);
        });
      }
    },
    // 编辑
    edit(scope) {
      this.title = '编辑用户';
      this.dialogFormVisible = true;
      this.form = { ...scope };
    },
    addUser() {
      instance.post('/users/add', this.form).then(res => {
        console.log(res);
        this.getlist();
        this.dialogFormVisible = false;
      }).catch(error => {
        console.error('添加失败:', error);
      });
    },
    // 注意：用户是不会去操作权限的，添加了也没有对应的路由规则
    showAdd() {
      this.title = '添加用户';
      this.dialogFormVisible = true;
      this.form = {};
    }
  },
  created() {
    this.getlist();
  },
  components: {
    Card
  }
}
</script>

<style lang="less" scoped>
.card_box[data-v-a53d947a] {
  overflow: hidden;
}
</style>    